package cn.edu.nju.ws.sview.reasoning;

import cn.edu.nju.ws.sview.rdf.Resource;

/**
 * Predicate
 * 
 * @author Gong Cheng
 * @author Gong Saisai
 */
public class Predicate {
	private Resource resource = null;
	private int arity;
    private int hash;
	/**
	 * 构建predicate
	 * 
	 * @param resource
	 *            该predicate的resource
	 * @param arity
	 *            该predicate的arity
	 */
	public Predicate(Resource resource, int arity) {
		this.resource = resource;
		this.arity = arity;
	}

	/**
	 * 根据已有Predicate构建一个新的Predicate
	 * @param predicate
	 */
	public Predicate(Predicate predicate){
		this.resource = predicate.getResource();
		this.arity = predicate.getArity();
	}
	
	/**
	 * 
	 * @return the predicate's resource
	 */
	public Resource getResource() {
		return this.resource;
	}

	/**
	 * 
	 * @return 该predicate的arity
	 */
	public int getArity() {
		return this.arity;
	}

	public boolean equals(Object predicate) {
		if (predicate instanceof Predicate) {
			Predicate predicateO = (Predicate) predicate;
			if (this.resource.equals(predicateO.resource)
					&& (this.arity == predicateO.arity)) {
				return true;
			}
		}
		return false;
	}

	public int hashCode() {
		int h = this.hash;
		if(h==0){
			h = this.resource.hashCode() + this.arity;
			this.hash = h;
		}
		return  h;
	}

	public String toString() {
		return this.resource.toString();
	}
}